import urllib.request
from lxml import etree

#     https://sc.chinaz.com/tupian/fengjingtupian.html
#     https://sc.chinaz.com/tupian/fengjingtupian_2.html
#     https://sc.chinaz.com/tupian/fengjingtupian_3.html
def create_request(page):
    if(page == 1):
        url = 'https://sc.chinaz.com/tupian/fengjingtupian.html'
    else:
        url = 'https://sc.chinaz.com/tupian/fengjingtupian_'+str(page)+'.html'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}

    print(url)

    request = urllib.request.Request(url=url,headers= headers)

    opens = urllib.request.urlopen(request)
    html = opens.read().decode('utf-8')

    return html

def create_eree(html):
    etreex = etree.HTML(html)
    name_list = etreex.xpath('//div[@class="item"]/img/@alt')

    url_list = etreex.xpath('//div[@class="item"]/img/@data-original')
    for i in range(len(url_list)):
        url = url_list[i]
        name = name_list[i]
        urllib.request.urlretrieve(url = 'https:'+url,filename='../下载的图片/'+name+'.jpg' )

        # print('https:'+url)





if __name__ == '__main__':
    start_page = int(input('请输入开始页码:'))
    end_page = int(input('请输入结束页码:'))

    for page in range(start_page,end_page+1):
        html = create_request(page)
        create_eree(html)
        print(page)